/* eslint-disable no-cond-assign */
'use static';

export default class Agent {
    constructor(logger) {
        this.Url(logger);
        this.Browser(logger);
        this.OS(logger);
        this.SCR(logger);
    }

    Browser(logger) {
        var browser_name = "";
        var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        var s;
        (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1]:
            (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
            (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
            (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
            (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
        if (Sys.ie) browser_name = 'IE: ' + Sys.ie;
        if (Sys.firefox) browser_name = 'Firefox: ' + Sys.firefox;
        if (Sys.chrome) browser_name = 'Chrome: ' + Sys.chrome;
        if (Sys.opera) browser_name = 'Opera: ' + Sys.opera;
        if (Sys.safari) browser_name = 'Safari: ' + Sys.safari;
        logger.push('_ua_', browser_name);
    }

    OS(logger) {
        var os = "";
        if (navigator.platform.indexOf("Win") > -1) {
            os += 'win'
        } else if (navigator.platform.indexOf("Mac") > -1) {
            os += "Mac";
        } else if (navigator.platform.indexOf("X11") > -1) {
            os += "Unix";
        } else if (navigator.platform.indexOf("Linux") > -1) {
            os += "Linux";
        } else {
            os += "Other";
        }
        logger.push('_os_', os);
    }

    SCR(logger) {
        logger.push("_scr_", window.screen.width + "*" + window.screen.height);
    }

    GetSID(c_name) {
        if (document.cookie.length > 0) {
            let c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1
                let c_end = document.cookie.indexOf(";", c_start)
                if (c_end == -1) c_end = document.cookie.length
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
    }
    Url(logger) {
        var title = document.title;
        if (title == null) {
            var t_titles = document.getElementByTagName("title");
            if (t_titles && t_titles.length > 0) {
                title = t_titles[0];
            } else {
                title = "";
            }
        }
        logger.push('_title_', title);
        var ref = window.location.href;
        let uid = window.userid;
        logger.push('_ref_', ref);
        logger.push('_url_', window.location.href);
        logger.push('_can_', document.cookie);
        logger.push('_userid_', uid);
        logger.push('_createtime_', Date.now());
    }
}